package uz.edo.db.bean;

import java.text.SimpleDateFormat;
import java.util.*;

import uz.edo.db.utils.DBObject;


public class User extends DBObject {
    private String userName;
    private String password;
    private Date startDate;
    private String startDateStr;
    private Date endDate;
    private String endDateStr;
    private String active;
    private Set roles;
    private String rolesStr;
    private Department department;
    private Map roleMap;
    private HashMap<?,?> rolehashmap;
    private Map privilegeMap;
    private List<Role> roleList;

    private String firstName;
    private String middleName;
    private String lastName;
    private String privLastName;
    private String email;
    private String phone;
    private String mobile;
    private Date dateOfBirth;
    private String birthPlace;
    private String livingPlace;
    private String inn;
    private String inps;
    private String nationality;
    private String sex;
    private String passportSeria;
    private String passportNumber;
    private Date dateOfIssue;
    private Date dateOfExpiry;
    private String authority;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    public Set getRoles() {
        return roles;
    }

    public void setRoles(Set roles) {
        this.roles = roles;
    }

    private Map getRoleMap(){
        roleMap = new HashMap();
        Iterator iterator = roles.iterator();
        while (iterator.hasNext()){
            Role role = (Role) iterator.next();
            roleMap.put(role.getName(), role);
        }
        return roleMap;
    }
    @SuppressWarnings({ "unchecked", "unused" })
	public HashMap getHashMapRole(){
    	HashMap rolehashmap_in = new HashMap();
        Iterator iterator = roles.iterator();
        while (iterator.hasNext()){
            Role role = (Role) iterator.next();
            rolehashmap_in.put(role.getName(), role);
        }
        return rolehashmap_in;
    }

    public Role getRole(String roleName){
        if(roleMap == null || roleMap.size() == 0)
            getRoleMap();
        return (Role)roleMap.get(roleName);
    }

    public List<Role> getRoleList() {
//        roleList = new LinkedList<Role>();
//        if(roles != null){
//            Iterator iterator = roles.iterator();
//            while (iterator.hasNext()){
//                Role role = (Role) iterator.next();
//                roleList.add(role);
//            }
//        }
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        if (roles == null)
            roles = new HashSet();
        for(Role role: roleList)
            roles.add(role);
        this.roleList = roleList;
    }

    public void loadRoleList(){
        roleList = new LinkedList<Role>();
        if(roles != null){
            Iterator iterator = roles.iterator();
            while (iterator.hasNext()){
                Role role = (Role) iterator.next();
                roleList.add(role);
            }
        }
    }

    public boolean isPrivileged(String name) {
        if(privilegeMap == null)
            privilegeMap = new HashMap();

        if(roleList == null){
            loadRoleList();
            if(roleList == null)
                return false;
        }

        if(privilegeMap.size() == 0){
            for (Role role : roleList){
                Set privileges = role.getPrivileges();
                if(privileges != null){
                    Iterator privilegeIterator = privileges.iterator();
                    while (privilegeIterator.hasNext()){
                        Privilege privilege = (Privilege)privilegeIterator.next();
                        privilegeMap.put(privilege.getName(),privilege);
                    }
                }
            }
        }
        return privilegeMap.get(name) != null;
    }

    public String getRolesStr() {
        Iterator iterator = roles.iterator();
        int i = 0;
        while (iterator.hasNext()){
            Role role = (Role)iterator.next();
            String value = role.getName();
            if(i++ > 0)
                rolesStr += ", " + value;
            else
                rolesStr = value;
        }
        return rolesStr;
    }

    public void setRolesStr(String rolesStr) {
        if(roles == null)
            roles = new HashSet<Privilege>();
        for(Role role : roleList)
            roles.add(role);
        this.rolesStr = rolesStr;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String getEndDateStr() {
        if (endDate != null)
            endDateStr = dateToStr(endDate);
        return endDateStr;
    }

    public void setEndDateStr(String endDateStr) {
        this.endDateStr = endDateStr;
    }

    public String getStartDateStr() {
        if (startDate != null)
            startDateStr = dateToStr(startDate);
        return startDateStr;
    }

    public void setStartDateStr(String startDateStr) {
        this.startDateStr = startDateStr;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPrivLastName() {
        return privLastName;
    }

    public void setPrivLastName(String privLastName) {
        this.privLastName = privLastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getBirthPlace() {
        return birthPlace;
    }

    public void setBirthPlace(String birthPlace) {
        this.birthPlace = birthPlace;
    }

    public String getLivingPlace() {
        return livingPlace;
    }

    public void setLivingPlace(String livingPlace) {
        this.livingPlace = livingPlace;
    }

    public String getInn() {
        return inn;
    }

    public void setInn(String inn) {
        this.inn = inn;
    }

    public String getInps() {
        return inps;
    }

    public void setInps(String inps) {
        this.inps = inps;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPassportSeria() {
        return passportSeria;
    }

    public void setPassportSeria(String passportSeria) {
        this.passportSeria = passportSeria;
    }

    public String getPassportNumber() {
        return passportNumber;
    }

    public void setPassportNumber(String passportNumber) {
        this.passportNumber = passportNumber;
    }

    public Date getDateOfIssue() {
        return dateOfIssue;
    }

    public void setDateOfIssue(Date dateOfIssue) {
        this.dateOfIssue = dateOfIssue;
    }

    public Date getDateOfExpiry() {
        return dateOfExpiry;
    }

    public void setDateOfExpiry(Date dateOfExpiry) {
        this.dateOfExpiry = dateOfExpiry;
    }

    public String getAuthority() {
        return authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }
}
